package myJs

import myJs.Utils._
import myJs.myPkg._
import myJs.Implicits._

import scala.collection.mutable.ArrayBuffer
import scala.scalajs.js
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.JSConverters._
import myJs.myPkg.jquery._
import scala.scalajs.js.Dynamic.{global => g}
import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by yz on 2019/4/15
  */
@JSExportTopLevel("MissionManage")
object MissionManage {

  val ids = ArrayBuffer[String]()

  @JSExport("init")
  def init = {
    $("#missionTable").bootstrapTable();
    updateMission { () =>
      bindEvt
    }


  }

  @JSExport("deleteMission")
  def deleteMission(id: String) = {
    val options = SwalOptions.title("").text("确定要删除此记录吗？").`type`("warning").showCancelButton(true).
      showConfirmButton(true).confirmButtonClass("btn-danger").confirmButtonText("确定").closeOnConfirm(false).
      cancelButtonText("取消").showLoaderOnConfirm(true)
    Swal.swal(options, () => {
      val url = g.jsRoutes.controllers.MissionController.deleteMissionById().url.toString
      MyFetch.get(url = s"${url}?missionId=${id}",
        headers = Map("Content-Type" -> "application/json")).map { any =>
        updateMission { () =>
          Swal.swal(SwalOptions.title("成功").text("删除成功").`type`("success"))
        }
      }.timeoutDeal

    })
  }

  @JSExport("updateMission")
  def updateMission(f: () => js.Any = () => ()) = {
    val url = g.jsRoutes.controllers.MissionController.getAllMission().url.toString
    MyFetch.get(url = s"${url}",
      headers = Map("Content-Type" -> "application/json")).map { any =>
      $("#missionTable").bootstrapTable("load", any)
      f()
    }.timeoutDeal
  }

  @JSExport("deleteAllInfos")
  def deleteAllInfos = {
    val options = SwalOptions.title("").text("确定要删除选中的记录吗？").`type`("warning").showCancelButton(true).
      showConfirmButton(true).confirmButtonClass("btn-danger").confirmButtonText("确定").closeOnConfirm(false).
      cancelButtonText("取消").showLoaderOnConfirm(true)
    Swal.swal(options, () => {
      val url = g.jsRoutes.controllers.MissionController.deleteMissionByIds().url.toString
      val data = js.Dictionary(
        "missionIds" -> ids.toJSArray
      )
      MyFetch.post(url = s"${url}", data = JSON.stringify(data), headers = Map("Content-Type" -> "application/json")).
        map { any =>
          updateMission { () =>
            Swal.swal(SwalOptions.title("成功").text("删除记录成功!").`type`("success"))
            getIds
          }
        }.timeoutDeal
    })
  }

  def getIds = {
    ids.clear()
    val arrays = $("#missionTable").bootstrapTable("getSelections").asInstanceOf[js.Array[js.Dictionary[String]]]
    ids ++= arrays.map { x =>
      x("id")
    }
    if (ids.isEmpty) $("#deleteButton").attr("disabled", true) else
      $("#deleteButton").attr("disabled", false)

  }

  def bindEvt = {
    $("#missionTable").on("check.bs.table", () => getIds)
    $("#missionTable").on("uncheck.bs.table", () => getIds)
    $("#missionTable").on("check-all.bs.table", () => getIds)
    $("#missionTable").on("uncheck-all.bs.table", () => getIds)
    $("#missionTable").on("page-change.bs.table", () => getIds)

  }


}
